package com.nursinghome.admin.service.impl;

import com.nursinghome.admin.mapper.SalaryMapper;
import com.nursinghome.admin.service.SalaryService;
import com.nursinghome.bean.Salary;
import com.nursinghome.exception.NurisingHomeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SalaryServiceImpl implements SalaryService {
    @Autowired
    private SalaryMapper salaryMapper;
    @Override
    public List<Salary> selectByPicday(String data) {
        return salaryMapper.selectByPicday(data);
    }

    @Override
    public Boolean insert(Salary salary) throws NurisingHomeException {
        Date payday = salary.getPayday();
        SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd");
        String data = smf.format(payday);
        if (salaryMapper.selectByMid(data,salary.getMid()) != null){
            throw new NurisingHomeException("该月该员工已经设置工资!无法重复设置");
        }
        return  salaryMapper.insert(salary) == 1;
    }

    @Override
    public Boolean update(Salary salary) throws NurisingHomeException {
        Long sid = salary.getSid();
        Salary bySid = salaryMapper.selectBySid(sid);
        if (bySid == null){
            throw new NurisingHomeException("没有找到该帐单");
        }
        return salaryMapper.update(salary) == 1;
    }

    @Override
    public Salary selectByMid(String data, Integer mid) {
        return salaryMapper.selectByMid(data, mid);
    }

}
